package com.dhec.ptms.epr.db;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhec.ptms.epr.model.CheckBenchMarkDetail;
import com.ionglobal.db.DBI;
import com.ionglobal.lib.SysException;

public class CheckBenchMarkDetailDB {
	
	private static final Logger logger = LoggerFactory.getLogger(CheckBenchMarkDetailDB.class);

	public List<CheckBenchMarkDetail> list(DBI dbi, int benchMarkId, int page, int pagesize){
		ArrayList<CheckBenchMarkDetail> list = new ArrayList<CheckBenchMarkDetail>(0);
		try {
			dbi.skipToPage(pagesize, page, "select * from EPR_CheckBenchMarkDetail where benchMarkId="+benchMarkId+" order by id asc");
			while(dbi.rsNext()){
				CheckBenchMarkDetail bean = new CheckBenchMarkDetail();
				this.handleData(dbi, bean);
				list.add(bean);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("CheckBenchMarkDetailDB.list(DBI, int, int, int) Error!", e);
		}
		
		return list;
		
	}
	
	public long count(DBI dbi, int benchMarkId){
		long c = 0;
		try {
			dbi.execQuery("select count(*) from EPR_CheckBenchMarkDetail where benchMarkId="+benchMarkId);
			if(dbi.rsNext()){
				c = dbi.rsGetLong(1);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("CheckBenchMarkDetailDB.list(DBI) Error!", e);
		}
		
		return c;
		
	}
	
	public void handleData(DBI dbi, CheckBenchMarkDetail bean) throws SysException{
		bean.setId(dbi.rsGetInt("id"));
		bean.setBenchMark(dbi.rsGetString("benchMark"));
		bean.setBenchMarkId(dbi.rsGetInt("benchMarkId"));
		bean.setCheckItem(dbi.rsGetString("checkItem"));
		bean.setCheckMethod(dbi.rsGetString("checkMethod"));
		bean.setType(dbi.rsGetString("type"));
	}
	
	
	public CheckBenchMarkDetail find(DBI dbi, int id){
		CheckBenchMarkDetail bean = null;
		try {
			dbi.execQuery("select * from EPR_CheckBenchMarkDetail where "
					+ "id="+id);
			if(dbi.rsNext()){
				bean = new CheckBenchMarkDetail();
				this.handleData(dbi, bean);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("CheckBenchMarkDetailDB.find(DBI, int) Error!", e);
		}
		
		return bean;
		
	}
	
	public boolean update(DBI dbi, CheckBenchMarkDetail bean){
		try {
			return dbi.execute("update EPR_CheckBenchMarkDetail set benchMark='"+dbi.fixSql(bean.getBenchMark())+"',"
					+ "benchMarkId="+bean.getBenchMarkId()+","
					+ "checkItem='"+dbi.fixSql(bean.getCheckItem())+"',"
					+ "checkMethod='"+dbi.fixSql(bean.getCheckMethod())+"',"
					+ "type='"+dbi.fixSql(bean.getType())+"',"
					+ " where id="+bean.getId());
		} catch (SysException e) {
			logger.error("CheckBenchMarkDetailDB.update(DBI, CheckBenchMarkDetail) Error!", e);
		}
		
		return false;
		
	}
	
	public boolean save(DBI dbi, CheckBenchMarkDetail bean){
		try {
			return dbi.execute("insert into EPR_CheckBenchMarkDetail (id, benchMarkId, checkItem, checkMethod, type, benchMark) values("+bean.getId()+","
					+ bean.getBenchMarkId()+","
					+ "'"+dbi.fixSql(bean.getCheckItem())+"',"
					+ "'"+dbi.fixSql(bean.getCheckMethod())+"',"
					+ "'"+dbi.fixSql(bean.getType())+"',"
					+ "'"+dbi.fixSql(bean.getBenchMark())+"')");
		} catch (SysException e) {
			logger.error("CheckBenchMarkDetailDB.save(DBI, CheckBenchMarkDetail) Error!", e);
		}
		
		return false;
		
	}
	
	public boolean delete(DBI dbi, int benchMarkId){
		try {
			return dbi.execute("delete from EPR_CheckBenchMarkDetail where benchMarkId="+benchMarkId);
		} catch (SysException e) {
			logger.error("CheckBenchMarkDetailDB.delete(DBI, int) Error!", e);
		}
		
		return false;
		
	}
}
